package com.ruoyi.coal.modules.coalpile.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import com.ruoyi.coal.common.utils.DateUtils;
import com.ruoyi.coal.common.utils.R;
import com.ruoyi.coal.modules.coalpile.domain.TableEntity;
import com.ruoyi.coal.modules.coalpile.service.TableService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"coalpile/table"})
@DataSource(value = DataSourceType.SLAVE)
public class TableController {
    @Resource
    private TableService tableService;

    @GetMapping({"/getTable"})
    public R getTable() {
        QueryWrapper<TableEntity> tableEntityQueryWrapper = new QueryWrapper();
        TableEntity table = (TableEntity)this.tableService.getOne((Wrapper)((QueryWrapper)tableEntityQueryWrapper.orderByDesc("update_time")).last("limit 1"));
        return R.ok().put("data", table);
    }

    @PostMapping({"/saveTable"})
    public R saveTable(@RequestBody TableEntity tableEntity) {
        String date = DateUtils.formatDate(new Date());
        tableEntity.setUpdateTime(date);
        this.tableService.save(tableEntity);
        return R.ok();
    }

    @PostMapping({"/updateTable"})
    public R updateTable(@RequestBody TableEntity tableEntity) {
        String date = DateUtils.formatDate(new Date());
        tableEntity.setUpdateTime(date);
        QueryWrapper<TableEntity> tableEntityQueryWrapper = new QueryWrapper();
        TableEntity table = (TableEntity)this.tableService.getOne((Wrapper)((QueryWrapper)tableEntityQueryWrapper.orderByDesc("update_time")).last("limit 1"));
        if (table == null) {
            this.tableService.save(tableEntity);
            return R.ok();
        }
        tableEntity.setId(table.getId());
        this.tableService.updateById(tableEntity);
        return R.ok();
    }

    @PostMapping({"/updateArray"})
    public R updateArray(@RequestBody TableEntity tableEntity) {
        String date = DateUtils.formatDate(new Date());
        tableEntity.setUpdateTime(date);
        QueryWrapper<TableEntity> tableEntityQueryWrapper = new QueryWrapper();
        TableEntity table = (TableEntity)this.tableService.getOne((Wrapper)((QueryWrapper)tableEntityQueryWrapper.orderByDesc("update_time")).last("limit 1"));
        tableEntity.setId(table.getId());
        table.setArrayList1(JSONObject.toJSONString(tableEntity.getArrayList1()));
        table.setArrayList2(JSONObject.toJSONString(tableEntity.getArrayList2()));
        table.setArrayList3(JSONObject.toJSONString(tableEntity.getArrayList3()));
        table.setArrayList4(JSONObject.toJSONString(tableEntity.getArrayList4()));
        table.setSubtextArray(JSONObject.toJSONString(tableEntity.getSubtextArray()));
        table.setColumnList(JSONObject.toJSONString(tableEntity.getColumnList()));
        this.tableService.updateById(table);
        return R.ok();
    }

    //    @PassToken
    @GetMapping({"/case/{total}"})
    public R caseTotal(@PathVariable String total) {
        QueryWrapper<TableEntity> tableEntityQueryWrapper = new QueryWrapper();
        TableEntity table = (TableEntity)this.tableService.getOne((Wrapper)((QueryWrapper)tableEntityQueryWrapper.orderByDesc("update_time")).last("limit 1"));
        List<String[]> list = JSONObject.parseArray((String)table.getArrayList3()).toJavaList(String[].class);
        int maxRowIndex = 0;
        double maxValue = Double.parseDouble(((String[])list.get(0))[0]);
        for (int i = 1; i < list.size(); i++) {
            try {
                double currentVal = Double.parseDouble(((String[])list.get(i))[0]);
                if (currentVal > maxValue) {
                    maxValue = currentVal;
                    maxRowIndex = i;
                }
            } catch (NumberFormatException e) {
                System.out.println("Value cannot be parsed to double: " + ((String[])list.get(i))[0]);
            }
        }
        System.out.println(list);
        return R.ok();
    }
}

